package com.oocl.o2o.merchant.util;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.apache.commons.codec.binary.Base64;
import org.springframework.util.Base64Utils;

import com.sun.mail.util.BASE64EncoderStream;

public class ImageUtil {
	// the base path of img location, using to save into disk
	private static String basePath;
	// the base url of img in network, using to get Url from network.
	private static String baseUrl;
	static {
		InputStream in = null;
		try {
			in = ImageUtil.class.getClassLoader().getResourceAsStream("image.properties");
			Properties properties = new Properties();
			properties.load(in);
			basePath = properties.getProperty("path");
			baseUrl = properties.getProperty("url");
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static String getAbsolutePath(String relativePath) {
		return basePath + relativePath;
	}

	public static String getAbosulteUrl(String relativeUrl) {
		return baseUrl + relativeUrl;
	}

	public static String getSuffix(String originalFileName) {
		int lastIndexOfDot = originalFileName.lastIndexOf('.');
		return originalFileName.substring(lastIndexOfDot);
	}
	
	public static String loadPic(String path) {
		FileInputStream in = null;
		try {
			in = new FileInputStream(path);
			byte[] buf = new byte[in.available()];
			in.read(buf);
			Base64 base64 = new Base64();
			return base64.encodeAsString(buf);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}finally {
			try {
				in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
	}
}
